package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfigJWT implements WebMvcConfigurer {
    @Autowired
    private WebHandlerIntercept webHandlerIntercept;


    /** 解决跨域问题 **/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600);
    }

    /**
     *静态资源处理
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /* 拦截器配置 */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(webHandlerIntercept)
                //拦截路径,/**表示所有
                .addPathPatterns("/**")
                //放行路径
//                .excludePathPatterns("/**");
        .excludePathPatterns("/login", "/static/**");
    }
}
